package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.po.Material;
import com.kxq.zhuoyue.service.MaterialService;
import com.kxq.zhuoyue.util.UUID;
import com.kxq.zhuoyue.util.result.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-06
 */
@Slf4j
@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;


    /**
     * 查询
     * @param page
     * @return
     */
    @PostMapping("/getByTitle")
    public AjaxResult getByTitle(@RequestBody Page page){
        Map map = new HashMap<>();
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Material::getMaterialTitle,page.getMaterialTitle());
        queryWrapper.eq(Material::getGroupId,page.getMaterialGroupId());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<Material> list = materialService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data", list);
        map.put("pageSize", pageInfo.getPageSize());
        map.put("total", pageInfo.getTotal());
        return AjaxResult.success("获取成功 !", map);
    }

    /**
     * 添加
     * @param material
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Material material){
        String id = UUID.getOrderIdByTime();
        material.setMaterialId(id);
        material.setMaterialStatus("未审核 ！");
        material.setMaterialViews("0");
        material.setMaterialCreateTime(new Date());
        boolean save = materialService.save(material);
        if (save){
            return AjaxResult.success("添加素材成功 ！");
        }
        else {
            return AjaxResult.success("添加素材失败 ！");
        }
    }

    /**
     * getById
     * @param materialId
     * @return
     */
    @GetMapping("/getById/{materialId}")
    public AjaxResult getById(@PathVariable String materialId){
        Material material = materialService.getById(materialId);
        return AjaxResult.success(material);
    }

    /**
     * 更新
     * @param material
     * @return
     */

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Material material){
        log.info(material.toString());
        boolean update = materialService.updateById(material);
        if (update){
            return AjaxResult.success("修改素材成功 ！");
        }
        else {
            return AjaxResult.success("修改素材失败 ！");
        }
    }

    @DeleteMapping("/delete/{materialId}")
    public AjaxResult delete (@PathVariable String materialId){
        boolean b = materialService.removeById(materialId);
        if (b){
            return AjaxResult.success("删除素材成功 ！");
        }
        else {
            return AjaxResult.success("删除素材失败 ！");
        }
    }

}

